package com.jinghang.logProject.utils

import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{LongType, StringType, StructField, StructType}

/**
  * 输入：访问时间、访问URL、耗费的流量、访问IP地址信息
  * 输出：URL、cmsType(video/article)、cmsId(编号)、流量、ip、城市信息、访问时间、天
  * 访问日志转换工具类 输入==>输出
  *
  * createTime: 2018-12-08 16:02
  **/
object LogConvertUtil {
  //定义输出的字段结构
  val structFieldArray = Array(
    StructField("url", StringType),
    StructField("cmsType", StringType),
    StructField("cmsId", LongType),
    StructField("traffic", LongType),
    StructField("ip", StringType),
    StructField("city", StringType),
    StructField("time", StringType),
    StructField("day", StringType)
  )

  val struct = StructType(structFieldArray)


  /**
    * 根据每一行输入的信息转换为输出的的样式
    *
    * @param log_format
    */
  def parseLog(log_format: String):Row = {
    try{
      //2016-11-10 00:01:02	http://www.imooc.com/code/1852/0	2345	117.35.88.11
      val splits = log_format.split("\t")

      val url = splits(1)
      val traffic = splits(2).toLong
      val ip = splits(3)

      val domain = "http://www.imooc.com/"

      //http://www.imooc.com/article/17891
      var cms = ""
      var cmsTypeAndIds = Array("")
      if (!url.equals("-")){
         cms = url.substring(url.indexOf(domain) + domain.length)
         cmsTypeAndIds = cms.split("/")
      }


      var cmsType = ""
      var cmsId = 0l
      if(cmsTypeAndIds.length > 1) {
        cmsType = cmsTypeAndIds(0)
        cmsId = cmsTypeAndIds(1).toLong
      }

     // val city = "项城市"
      val city = IpUtils.getCity(ip)
      val time = splits(0)
      //2018-05-11 20:56:29
      val day = time
        .substring(0,10) //2018-05-11
        .replaceAll("-","")//20180511

      //这个row里面的字段要和struct中的字段对应上
      Row(url, cmsType, cmsId, traffic, ip, city, time, day)
    } catch {
      case e:Exception => Row("","",0L,0L,"","","","")
    }
  }


  def main(args: Array[String]): Unit = {

    val url = "fdgfd http://www.imooc.com/article/17891"
    val domain = "http://www.imooc.com/"

    val index = url.indexOf(domain)
    println(index)

    val index_2 = index + domain.length
    val cms = url.substring(index_2)
    println(index_2)
    println(cms)//  article/17891
    val cmsTypeAndIds = cms.split("/")
  }

}
